package view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import darksideoftheloom.Attendee;
import darksideoftheloom.Event;
import darksideoftheloom.Judge;
import darksideoftheloom.Submission;

/**
 * This class shows judges a list of entries to identify the winner.
 * 
 * @author Shane Kwon
 * @version 2013-03-09
 */
@SuppressWarnings("serial")
public class BeweaveJudgePanel extends JPanel {
    /**
     * The width of the panel.
     */
    private static final int WIDTH = 1024;

    /**
     * The height of the panel.
     */
    private static final int HEIGHT = 768;

    /**
     * The number of columns in the display table.
     */
    private static final int COLS = 3;

    /**
     * The logged in judge.
     */
    private Judge my_judge;

    /**
     * The center panel.
     */
    private final JPanel center_panel = new JPanel();

    /**
     * The weaving event that is being judged.
     */
    private final Event my_event;

    /**
     * The bottom panel a.k.a. south panel.
     */
    private final JPanel bottom_panel = new JPanel();

    /**
     * Constructs the judge panel that displays a list of entries for their
     * category.
     * 
     * @param the_weavent
     *            The weaving Event
     * @param the_panel
     *            the panel container, used for switching between panels
     */
    public BeweaveJudgePanel(final Event the_event, final JPanel the_panel) {
        super();
        setLayout(new BorderLayout());
        my_event = the_event;
        final JButton logout_button = new JButton(new AbstractAction("Logout") {
            public void actionPerformed(final ActionEvent the_event) {
                my_judge = null;
                CardLayout cl = (CardLayout) the_panel.getLayout();
                cl.show(the_panel, "login");
            }
        });

        bottom_panel.add(logout_button);
        add(bottom_panel, BorderLayout.SOUTH);

        setPreferredSize(new Dimension(WIDTH, HEIGHT));
    }

    /**
     * Sets the judge user on successful login.
     * 
     * @param the_judge
     *            the Judge that is logged in.
     */
    public void login(final Judge the_judge) {
        my_judge = the_judge;
        showEntries();
    }

    /**
     * Populates the panel with entry/submission information.
     */
    public void showEntries() {
        center_panel.removeAll();
        List<Submission> submissions = new ArrayList<Submission>();
        for (Attendee a : my_event.getAttendees()) {
            submissions.addAll(a.getSubmissions());
        }

        for (Submission s : submissions) {
            if (!s.getCategory().equals(my_judge.getCategory())) {
                submissions.remove(s);
            }
        }

        center_panel.setLayout(new BorderLayout());
        String[] column_names = { "Category", "Design Name", "User" };
        Object[][] data = new Object[submissions.size()][COLS];
        for (int i = 0; i < submissions.size(); i++) {
            Submission s = submissions.get(i);
            data[i][0] = s.getCategory();
            data[i][1] = s.getDesignName();
            data[i][2] = s.getUserName();
        }
        JTable table = new JTable(data, column_names);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        center_panel.add(scrollPane, BorderLayout.CENTER);

        add(center_panel, BorderLayout.CENTER);
    }
}
